package com.dinetech.dao;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.dinetech.entidades.Personal;
import com.dinetech.entidades.Session;
import com.dinetech.entidades.SpinnerVarios;

public class WSPersonal {

	
	final String NAMESPACE = "http://www.CuyTechService.pe/WSservice2005/";//"http://192.168.1.10/WebService/"; 
	final String URL= "http://www.cuytechservice.pe/WsCuyTech2005/SoapDataServices.asmx";//"http://192.168.1.10/WSservice2005/SoapDataServices.asmx";
	private String resultado;
	
	
	
	public String wcf (String usu, String clave){
				
		final String METHOD_NAME = "Validar";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    		
		request.addProperty("usuario", usu);
		request.addProperty("clave", clave);							
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		
			HttpTransportSE transporte = new HttpTransportSE(URL);
			
			
			try{
			transporte.call(SOAP_ACTION, envelope);
			
			SoapObject respSoap=(SoapObject)envelope.getResponse();
			Personal[] listap= new Personal[respSoap.getPropertyCount()];
			
			
			for(int i=0; i<listap.length; i++){
				
				SoapObject ic =(SoapObject)respSoap.getProperty(i);
				
				//Guarda los eventos dentro de la sesion para usarlo en la app
				if(ic.getPropertyAsString("Msg").equals("0")){	
					
					
					int idSuc= Integer.parseInt(ic.getPropertyAsString("IdSucursal"));
					int idGranja= Integer.parseInt(ic.getPropertyAsString("IdGranja"));
					WSGalpon wsG = new WSGalpon();
					WSEvento wsE = new WSEvento();
					WSProgramacion wsP = new WSProgramacion();
					
					Session swf= Session.getInstance();
					
					swf.setLISTA_GALPONES(wsG.WSListarGalpon(idSuc));
					swf.setLISTA_EVENTO(wsE.listarEventos("01"));
					swf.setLISTA_EVENTO_REPRODUCTOR(wsE.listarEventos("02"));
					swf.setLISTA_GENERO(listaCbxSession(15));
					swf.setLISTA_LINEA_GENETICA(listaCbxSession(16));
					swf.setLISTA_TIPO_SELECCION(listaCbxSession(28));
					swf.setLISTA_VIA_APLICACION(listaCbxSession(32));
					swf.setLISTA_TIPO_EMPADRE(listaCbxSubTipoEvento(13, "01"));					
					swf.setLISTA_RESPONSABLE(listar_Personal_CbxSession(idSuc));
					swf.setLISTA_TIPO_TRATAMIENTO(wsE.listarTipoTratamiento(idGranja));
					swf.setLISTA_ACTIVIDADES(wsP.listarListViewActividades(Integer.parseInt(ic.getPropertyAsString("IdPersonal")), swf.obtenerFechaActual()));

				}
				
				Personal per = Personal.getInstance();
				per.setIdPersonal(Integer.parseInt( ic.getProperty(14).toString()));
				per.setIdGranja(Integer.parseInt( ic.getProperty(11).toString()));
				per.setIdSucursal(Integer.parseInt( ic.getProperty(15).toString()));
				per.setNombreUsuario( ic.getProperty(16).toString());
				
				
				resultado= ic.getProperty(17).toString();
				
			}

			}catch(Exception e){
				resultado=e.getMessage().toString();
			}			
		
		return resultado;
	}
	
	
	public ArrayList<SpinnerVarios> listaCbxSession(int IdMaeTabla){
		
		final String METHOD_NAME = "ListarVarios";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		ArrayList<SpinnerVarios> listaV = new ArrayList<SpinnerVarios>();
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    		
		request.addProperty("IdMaeTabla", IdMaeTabla);								
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);				
		HttpTransportSE transporte = new HttpTransportSE(URL);		
			
			try{
			transporte.call(SOAP_ACTION, envelope);
			
			SoapObject respSoap=(SoapObject)envelope.getResponse();

			for(int i=0; i < respSoap.getPropertyCount(); i++){
				
				SoapObject ic =(SoapObject)respSoap.getProperty(i);

				listaV.add(new SpinnerVarios(Integer.parseInt(ic.getPropertyAsString("Valor")), ic.getPropertyAsString("Descripcion")));
				
			}

			}catch(Exception e){
				resultado=e.getMessage().toString();
			}			
		
		return listaV;
	}
	
	public ArrayList<SpinnerVarios> listaCbxSubTipoEvento(int IdMaeTabla, String IdEvento){
		
		final String METHOD_NAME = "ListarSubTipoEvento";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		ArrayList<SpinnerVarios> listaV = new ArrayList<SpinnerVarios>();
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    		
		request.addProperty("IdMaeTabla", IdMaeTabla);	
		request.addProperty("IdTipoEvento", IdEvento);	
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);				
		HttpTransportSE transporte = new HttpTransportSE(URL);		
			
			try{
			transporte.call(SOAP_ACTION, envelope);
			
			SoapObject respSoap=(SoapObject)envelope.getResponse();

			for(int i=0; i < respSoap.getPropertyCount(); i++){
				
				SoapObject ic =(SoapObject)respSoap.getProperty(i);

				listaV.add(new SpinnerVarios(Integer.parseInt(ic.getPropertyAsString("Valor")), ic.getPropertyAsString("Descripcion")));
				
			}

			}catch(Exception e){
				resultado=e.getMessage().toString();
			}			
		
		return listaV;
	}

	public ArrayList<SpinnerVarios> listar_Personal_CbxSession (int idsucursal){
		
		ArrayList<SpinnerVarios> listaArray= new ArrayList<SpinnerVarios>();
	    		
		final String METHOD_NAME = "listar_Personal_Cbx";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    		
		request.addProperty("Idsucursal", idsucursal);							
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		
			HttpTransportSE transporte = new HttpTransportSE(URL, 5000);
			
			
			try{
				transporte.call(SOAP_ACTION, envelope);				
				SoapObject respSoap=(SoapObject)envelope.getResponse();
				
				for(int i=0; i < respSoap.getPropertyCount(); i++){
					
					SoapObject ic =(SoapObject)respSoap.getProperty(i);
					listaArray.add(new SpinnerVarios(Integer.parseInt( ic.getPropertyAsString("IdPersonal")),ic.getPropertyAsString("Nombres").toString()));				
				}
			
			}catch(Exception e){
				resultado=e.getMessage().toString();
			}			
		
		return listaArray ;
	}

	
}
